package ninder.system.view.beans;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.apache.poi.util.IOUtils;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@ManagedBean
@ViewScoped
public class UploadMaterialDeApoioBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	

	private LoginBean login;
	
	
	
	private String nucleos;
	 
	private String disci;
	
	
	
	public UploadMaterialDeApoioBean(){
		
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		HttpSession session = (HttpSession) externalContext.getSession(true);
		login = (LoginBean) session.getAttribute("loginBean");
		        
      		
	}
	



	/**
	 * @return the disci
	 */
	public String getDisci() {
		return disci;
	}


	/**
	 * @param disci the disci to set
	 */
	public void setDisci(String disci) {
		this.disci = disci;
	}



	public void fileUploadAction(FileUploadEvent event) {
        try {
           

            FacesContext aFacesContext = FacesContext.getCurrentInstance();
            ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext();
           
            String realPath = context.getRealPath("/");
            
            
            // Aqui cria o diretorio caso nao exista
            File file = new File(realPath + "/documentos/");
            file.mkdirs();
            
            UploadedFile arq = event.getFile();
          	 
        	InputStream in = new BufferedInputStream(arq.getInputstream());
        	
        	byte[] arquivo = IOUtils.toByteArray(in);
            
        	String caminho = realPath + "/documentos/" + event.getFile().getFileName();    
      
           // esse trecho grava o arquivo no diretorio
        	
        	login.setIndex(login.getIndex()+1);
        	
        	int index = login.getIndex();
        	
            FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()+"/"+index+".jpg");
            fos.write(arquivo);
            fos.close();
            
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucesso", "Transferencia concluida para o servido!");  
	        
		    RequestContext.getCurrentInstance().showMessageInDialog(message);

        } catch (Exception ex) {
            
        	FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, "ERRO", "Transferencia Nao concluida para o servido!");  
	        
		    RequestContext.getCurrentInstance().showMessageInDialog(message);
            System.out.println("Erro no upload de imagem" + ex);
        }
    }
	
	public void handleCityChange2(final AjaxBehaviorEvent event) {
		
	}
	
	
 
    public void displayLocation() {
       
    }



	public LoginBean getLogin() {
		return login;
	}



	public void setLogin(LoginBean login) {
		this.login = login;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public String getNucleos() {
		return nucleos;
	}



	public void setNucleos(String nucleos) {
		this.nucleos = nucleos;
	}



	
	

}
